package main

import (
	"fmt"
	"reflect"
)

func main() {
	//for init; condition; post { }
	//无法显式声明类型并赋值，就是这么规定的；
	var i int32
	for i = 0; i < 5; i++ {
		fmt.Printf("类似C的for循环，初始值在循环外定义 %d", i)
	}
	fmt.Println()
	//允许短变量声明并赋值
	for j := 5; j > 0; j-- {
		if j%2 == 0 {
			//存在continue
			continue
		}
		fmt.Printf("类似C的for循环，初始值在循环内定义，循环内定义必须遵守GO规矩 %d", j)
	}
	fmt.Println()
	var k int32 = 5
MARK1:
	for k > 0 {
		if k < 3 {
			//存在goto
			k--
			goto MARK1
		}
		fmt.Printf("类似C的while循环，初始值定义在循环外 %d", k)
		k--
	}
	fmt.Println()
	//简写
	var m int32 = 5
MARK2:
	for m > 0 {
		if m < 3 {
			//存在break
			m--
			goto MARK2
		}
		fmt.Printf("类似C的while循环，初始值定义在循环外 %d", m)
		m--
	}
	fmt.Println()
	var l int32 = 0
	for {
		if l == 3 {
			//存在break
			break
		}
		fmt.Printf("类似C的for(;;)循环，无限循环 %d", l)
		l++
	}
	fmt.Println()
	//简写
	var n int32 = 0
	for {
		if n == 3 {
			//存在break
			break
		}
		fmt.Printf("类似C的for(;;)循环，无限循环 %d", n)
		n++
	}
	fmt.Println()
	var array1 = [5]int32{10, 20, 30, 40, 50}
	//range遍历切片，for循环中不允许显式声明类型并赋值，必须使用短变量声明并赋值
	for key1, value1 := range array1 {
		fmt.Printf("切片array1的索引 %d 的元素的值为 %d", key1, value1)
	}
	fmt.Println()
	for _, value2 := range array1 {
		fmt.Printf("遍历切片array1,当前值 %d", value2)
	}
	fmt.Println()
	for key2, _ := range array1 {
		fmt.Printf("遍历切片array1的索引,当前索引 %d", key2)
	}
	fmt.Println()
	//这种写法只能读取索引，无法读取元素的值
	for key3 := range array1 {
		fmt.Printf("遍历切片array1的索引,当前索引 %d", key3)
	}
	fmt.Println()
	var key4 interface{}
	var value4 interface{}
	for key4, value4 = range array1 {
		fmt.Printf("reflect.TypeOf(key4) = %v, reflect.TypeOf(value4) = %v\n", reflect.TypeOf(key4), reflect.TypeOf(value4))
	}
}
